package com.itbaima.exception;

import com.itbaima.entity.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyControllerAdvice {

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseResult handleException(Exception e) {
        // 获取异常信息 存入ResponseResult的msg属性
        String message = e.getMessage();
        ResponseResult result = new ResponseResult(300, message);
        // 把ResponseResult 作为返回值返回 要求转换成json格式存入响应体重
        return result;
    }
}
